import 'dart:math';

import 'package:flutter/material.dart';

class GoodsTypePage extends StatelessWidget {
  const GoodsTypePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: CustomScrollView(slivers: [
        const SliverAppBar(
          title: Text("232323"),
          leading: CloseButton(),
          expandedHeight: 230.0,
          pinned: true,
        ),
        initSliverPersistentHeader("2323"),
        initSliverList()
      ]),
    );
  }

  Widget initSliverList() {
    return SliverList(
      delegate: SliverChildBuilderDelegate((context, index) {
        return Container(
          height: 100,
          alignment: Alignment.center,
          child: Text(
            "SliverList:$index",
            textAlign: TextAlign.center,
          ),
        );
      }, childCount: 20),
    );
  }

  Widget initSliverPersistentHeader(String title) {
    return SliverPersistentHeader(
        //是否固定头布局 默认false
        pinned: true,
        //是否浮动 默认false
        floating: false,
        delegate: MySliverDelegate(
          //缩小后的布局高度
          minHeight: 40.0,
          //展开后的高度
          maxHeight: 80.0,
          child: Container(
              color: Colors.redAccent,
              child: Center(
                child: Text(
                  title,
                  //style: const TextStyle(fontSize: 18, shadows: [Shadow(color: Colors.white, offset: Offset(1, 1))]),
                  style: const TextStyle(fontSize: 18),
                ),
              )),
        ));
  }
}

class MySliverDelegate extends SliverPersistentHeaderDelegate {
  MySliverDelegate({
    required this.minHeight,
    required this.maxHeight,
    required this.child,
  });

  final double minHeight; //最小高度
  final double maxHeight; //最大高度
  final Widget child; //子Widget布局

  @override
  double get minExtent => minHeight;

  @override
  double get maxExtent => max(maxHeight, minHeight);

  @override
  Widget build(
      BuildContext context, double shrinkOffset, bool overlapsContent) {
    return SizedBox.expand(child: child);
  }

  @override //是否需要重建
  bool shouldRebuild(MySliverDelegate oldDelegate) {
    return maxHeight != oldDelegate.maxHeight ||
        minHeight != oldDelegate.minHeight ||
        child != oldDelegate.child;
  }
}
